{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": 2,
      "metadata": {
        "id": "cLkNm1Ywb3Eh"
      },
      "outputs": [],
      "source": [
        "# Load libraries\n",
        "from sklearn import datasets\n",
        "from sklearn import metrics\n",
        "from sklearn.model_selection import KFold, cross_val_score\n",
        "from sklearn.pipeline import make_pipeline\n",
        "from sklearn.linear_model import LogisticRegression\n",
        "from sklearn.preprocessing import StandardScaler"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "metadata": {
        "id": "TvR-6-N55ZGJ"
      },
      "outputs": [],
      "source": [
        "# digits dataset loading\n",
        "digits = datasets.load_digits()\n",
        "# Create features matrix\n",
        "features = digits.data\n",
        "# Create target vector\n",
        "target = digits.target"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "metadata": {
        "id": "zlsG8vbu5cqh"
      },
      "outputs": [],
      "source": [
        "# standardization\n",
        "standard_scaler = StandardScaler()\n",
        "# logistic regression creation\n",
        "logit = LogisticRegression()"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "4LcbDlYC5m-E",
        "outputId": "dc10db6a-272d-4a37-d083-758b4a83dcfb"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "array([0.92682927, 0.98170732, 0.95731707, 0.95121951, 0.98159509,\n",
              "       0.97546012, 0.98159509, 0.98773006, 0.96319018, 0.97546012,\n",
              "       0.96932515])"
            ]
          },
          "execution_count": 8,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# pipeline creation for standardization and performing logistic regression\n",
        "pipeline = make_pipeline(standard_scaler, logit)\n",
        "# perform k-Fold cross-validation\n",
        "kf = KFold(n_splits=11, shuffle=True, random_state=2)\n",
        "# k-fold cross-validation conduction\n",
        "cv_results = cross_val_score(pipeline, # Pipeline\n",
        "                             features, # Feature matrix\n",
        "                             target, # Target vector\n",
        "                             cv=kf, # Cross-validation technique\n",
        "                             scoring=\"accuracy\", # Loss function\n",
        "                             n_jobs=-1) # Use all CPU cores\n",
        "# View score for all 11 folds\n",
        "cv_results"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "metadata": {
        "colab": {
          "base_uri": "https://localhost:8080/"
        },
        "id": "hdX0sbfBaWsI",
        "outputId": "9fdc89ce-c2f7-432d-8c6a-35a65f751066"
      },
      "outputs": [
        {
          "data": {
            "text/plain": [
              "0.968311727177506"
            ]
          },
          "execution_count": 9,
          "metadata": {},
          "output_type": "execute_result"
        }
      ],
      "source": [
        "# Calculate mean\n",
        "cv_results.mean()"
      ]
    }
  ],
  "metadata": {
    "colab": {
      "name": "CrossValidation-ScikitLearn_PythonCodeTutorial.ipynb",
      "provenance": []
    },
    "kernelspec": {
      "display_name": "Python 3",
      "name": "python3"
    },
    "language_info": {
      "name": "python"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}
